//
//  ZFForgetPswViewController.m
//  HappilyLoan
//
//  Created by zainishenbian on 2017/3/22.
//  Copyright © 2017年 zongqingxiangqian. All rights reserved.
//

#import "ZFForgetPswViewController.h"
#import "ZFLoginHttpRequest.h"
#import "UIButton+countDown.h"
#import "ZFHeader.h"

@interface ZFForgetPswViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)AlertBoxView *alertBox;
@end

@implementation ZFForgetPswViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"密码找回";
    [self.view addSubview:self.alertBox];
}
#pragma mark ———— textfield delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // Check for total length
    NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
    
    if (textField == _phoneTF) {
        
        if (proposedNewLength > 11) {
            
            return NO;
        }
    }
    if (textField == _verifyTF) {
        
        if (proposedNewLength > 6) {
            
            return NO;
        }
    }
    if (textField == _pswTF) {
        
        NSUInteger lengthOfString = string.length;  //lengthOfString的值始终为1
        for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++) {
            unichar character = [string characterAtIndex:loopIndex]; //将输入的值转化为ASCII值（即内部索引值），可以参考ASCII表
            // 48-57;{0,9};65-90;{A..Z};97-122:{a..z}
            if (character < 48) return NO; // 48 unichar for 0
            if (character > 57 && character < 65) return NO; //
            if (character > 90 && character < 97) return NO;
            if (character > 122) return NO;
            
        }
        if (proposedNewLength > 16) {
            return NO;//限制长度
        }
    }
    return YES;
}

#pragma mark ———— button的点击事件
//获取验证码
- (IBAction)getVerifyCode:(id)sender {
    
    UIButton *button = (UIButton *)sender;
    
    
    if ([ZFLoginConfig isValidatePhone:[ZFPulicAPI replaceBlank:_phoneTF.text]]) {
        
        [button startWithTime:60 title:@"重获验证码" countDownTitle:@"s后重新获取" mainColor:Main_Color1 countColor:[UIColor lightGrayColor]];
        
        [ZFLoginHttpRequest requestGetSmsCodePhone:[ZFPulicAPI replaceBlank:_phoneTF.text] withCodeType:@"3" sucessBlock:^(NSDictionary *dict) {
            
            if ([dict[@"code"] integerValue] == 1) {
                
                //NSLog(@"获取验证码成功");
                [_alertBox appearAndDisappearAlertWithString:[NSString stringWithFormat:@"%@",dict[@"msg"]]];
            }
            else
            {
                
                [_alertBox appearAndDisappearAlertWithString:[NSString stringWithFormat:@"%@",dict[@"msg"]]];
            }

        } failureBlock:^(NSInteger failSign) {
            
            [_alertBox appearAndDisappearAlertWithString:@"网络连接失败"];
            
        }];
    }else{
        UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:nil message:@"请输入正确的手机号" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertV show];
    }
}
//完成
- (IBAction)competeButton:(id)sender {
    if (self.phoneTF.text.length == 0) {
        [_alertBox appearAndDisappearAlertWithString:@"手机号不能为空"];
        return;
    }
    else if (self.verifyTF.text.length == 0) {
        [_alertBox appearAndDisappearAlertWithString:@"验证码不能为空"];
        return;
    }
    else if (self.pswTF.text.length == 0) {
        [_alertBox appearAndDisappearAlertWithString:@"新密码不能为空"];

        return;
    }
    if ([ZFLoginConfig isValidatePhone:[ZFPulicAPI replaceBlank:_phoneTF.text]]){
        
    if ([ZFPulicAPI replaceBlank:_phoneTF.text].length >= 11 && [ZFPulicAPI replaceBlank:_verifyTF.text].length >= 6 && [ZFPulicAPI replaceBlank:_pswTF.text].length >= 6) {
        
        
        [ZFLoginHttpRequest requestFixPwdPhone:[ZFPulicAPI replaceBlank:_phoneTF.text] smsCode:[ZFPulicAPI replaceBlank:_verifyTF.text] pwd:[ZFPulicAPI replaceBlank:_pswTF.text] sucessBlock:^(NSDictionary *dict) {
            
            if ([dict[@"code"] integerValue] == 1) {
                
                
                [_alertBox appearAndDisappearAlertWithString:[NSString stringWithFormat:@"%@",dict[@"msg"]]];
                [self.navigationController popViewControllerAnimated:YES];
            }
            else
            {
             
                [_alertBox appearAndDisappearAlertWithString:[NSString stringWithFormat:@"%@",dict[@"msg"]]];
            }
        } failureBlock:^(NSInteger failSign) {
            [_alertBox appearAndDisappearAlertWithString:@"网络连接失败"];
        }];
        }else{
            [_alertBox appearAndDisappearAlertWithString:@"请填写正确的手机号"];
        }
    }
    else{
        [_alertBox appearAndDisappearAlertWithString:@"请填写正确的手机号"];
    }
}
- (AlertBoxView *)alertBox
{
    if (!_alertBox) {
        _alertBox = [[AlertBoxView alloc] init];
    }
    return _alertBox;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"forgetPass"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"forgetPass"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
